package com.hursing.tractor.controller;

import java.util.ArrayList;
import java.util.List;

import android.os.Handler;
import android.os.Message;

import com.hursing.tractor.baseview.UIView;
import com.hursing.tractor.model.Card;
import com.hursing.tractor.model.GameData;

public final class KittyStage extends Stage implements KittyController {

	private static final int MESSAGE_BURY = 0;
	private static final int MESSAGE_START_PLAY = 1;
	private static final int INTERVAL = 1;
	
	private final Handler mKittyHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case MESSAGE_BURY:
				notifyBuryKitty();
				break;
			case MESSAGE_START_PLAY:
				nextStage();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	void start() {
		notifyDrawKitty();
	}
	
	private void nextKittyStep() {
		if (GameData.instance().kittyCards().size() == 0) {
			mKittyHandler.sendEmptyMessageDelayed(MESSAGE_BURY, INTERVAL);
		} else {
			mKittyHandler.sendEmptyMessageDelayed(MESSAGE_START_PLAY, INTERVAL);
		}
	}
	
	/**
	 * 通知拿底牌
	 */
	private void notifyDrawKitty() {
		GameData.instance().housePositionPlayer().drawKitty(this);
	}
	
	/**
	 * 发底牌
	 */
	@Override
	public List<Card> dealKitty() {
		List<Card> kitty = new ArrayList<Card>(GameData.instance().kittyCards());
		GameData.instance().kittyCards().clear();
		nextKittyStep();
		return kitty;
	}
	
	/**
	 * 通知扣底
	 */
	private void notifyBuryKitty() {
		GameData.instance().clearPlayedCards();
		GameData.instance().housePositionPlayer().buryKitty(this);
	}
	
	/**
	 * 收底牌
	 * @param kitty
	 */
	@Override
	public void collectKitty(List<Card> kitty) {
		GameData.instance().kittyCards().addAll(kitty);
		UIView.sViews[UIView.VIEW_KITTY_CARD].toCardView().cards(kitty);
		nextKittyStep();
	}

	@Override
	void pause() {
		
	}

	@Override
	void recover() {
		
	}
}
